//package com.kevin.controller.aspect;
//
//import com.kevin.common.exception.BusinessException;
//import lombok.SneakyThrows;
//import lombok.extern.slf4j.Slf4j;
//import org.redisson.api.RLock;
//import org.redisson.api.RedissonClient;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import java.util.concurrent.TimeUnit;
//import java.util.function.Supplier;
//
//@Service
//@Slf4j
//public class LockService {
//
//    @Autowired
//    private RedissonClient redissonClient;
//
//    public <T> T executeWithLockThrows(String key, int waitTime, TimeUnit unit, SupplierThrow<T> supplier) throws Throwable {
//        RLock lock = redissonClient.getLock(key);
//        boolean lockSuccess = lock.tryLock(waitTime, unit);
//        if (!lockSuccess) {
//            throw new BusinessException("请求太频繁了，请稍后再试哦~~");
//        }
//        try {
//            return supplier.get();//执行锁内的代码逻辑
//        } finally {
//            lock.unlock();
//        }
//    }
//
//    @SneakyThrows
//    public <T> T executeWithLock(String key, int waitTime, TimeUnit unit, Supplier<T> supplier) {
//        return executeWithLockThrows(key, waitTime, unit, supplier::get);
//    }
//
//    @FunctionalInterface
//    public interface SupplierThrow<T> {
//
//        /**
//         * Gets a result.
//         *
//         * @return a result
//         */
//        T get() throws Throwable;
//    }
//}
